package com.hzqy.service.warn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.JsonUtils;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.persistence.dao.warn.WarnDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.CmPicturesVo;
import com.hzqy.web.vo.SysWarnVo;

@Service
public class WarnServiceBean extends GenericServiceBean<SysWarnVo, WarnDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private WarnDao warnDao;
	@Resource
	private PictureDao pictureDao;
	@Override
	public WarnDao getEntityDao() {
		return warnDao;
	}
	
	
	/**
	 * 根据id查询公告详情
	 * @param F_sw_id
	 * @return
	 */
	public SysWarnVo selectSysWarn(String f_sw_id) {
		return warnDao.selectSysWarn(f_sw_id);
	}
	/**
	 * 	公告列表获取
	 * @param SysWarnVo
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public Map<String,Object> selectSysWarnList(SysWarnVo sysWarnVo, int curPage,
			int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = warnDao.selectSysWarnCount(sysWarnVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_sw_status", sysWarnVo.getF_sw_status());
			map.put("f_sw_name", sysWarnVo.getF_sw_name());
			map.put("f_sw_type", sysWarnVo.getF_sw_type());
			List<SysWarnVo> results = warnDao.selectSysWarnList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<SysWarnVo>());
		}
		return map;
	}
	
	
	/**
	 * 删除公告
	 * @param f_sw_id
	 * @return
	 */
	public int deleteSysWarn(String f_sw_id) {
		warnDao.deleteSysWarn(f_sw_id);
		return 1;
	}
	
	public int batchDeleteWarn(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			flag += warnDao.deleteSysWarn(idsArr[i]);
		}
		return flag;
	}
	
	/**
	 * 增加公告
	 * @param sysWarnVo
	 * @return
	 */
	public Map<String,Object> insertSysWarn(SysWarnVo sysWarnVo,String picJson) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = 0;
		if(sysWarnVo==null || sysWarnVo.getF_sw_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
				List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
				warnDao.insertSysWarn(sysWarnVo);
				String F_sw_id = sysWarnVo.getF_sw_id();
				for(int i=0;i<picList.size();i++) {
					CmPicturesVo cmPicturesVo = new CmPicturesVo();
					cmPicturesVo = picList.get(i);
					cmPicturesVo.setF_cp_relationid(Integer.parseInt(F_sw_id));
					pictureDao.insertPic(cmPicturesVo);
				}
				resultMap.put("success", true);
				resultMap.put("result","新增公告成功!");
				resultMap.put("F_sw_id",F_sw_id);
		}else{
			resultMap.put("success", false);
			resultMap.put("result","新增公告失败!");
		}
		return resultMap;
	}
	/**
	 * 修改公告
	 * @param sysWarnVo
	 * @return
	 */
	public Map<String,Object> updateSysWarn(SysWarnVo sysWarnVo,String picJson,String picType) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = 0;
		if(sysWarnVo==null || sysWarnVo.getF_sw_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
			String listPicId = JsonUtils.jsonToPicIdList(picJson);
			String f_cv_relationid = sysWarnVo.getF_sw_id();
			Map<String,Object> picMap = new HashMap<String, Object>();
			picMap.put("listPicId", listPicId);
			picMap.put("f_cp_relationid", f_cv_relationid);
			picMap.put("picType", picType);
			
			warnDao.updateSysWarn(sysWarnVo);
			pictureDao.deletePic(picMap);
			for(int i=0;i<picList.size();i++) {
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo = picList.get(i);
				cmPicturesVo.setF_cp_relationid(Integer.parseInt(f_cv_relationid));
				pictureDao.insertPic(cmPicturesVo);
			}
			resultMap.put("success", true);
			resultMap.put("result","修改公告成功!");
		}else{
			resultMap.put("success", false);
			resultMap.put("result","修改公告失败!");
		}
		return resultMap;
	}
	public int batchReviewSysWarn(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			SysWarnVo sysWarnVo = new SysWarnVo();
			sysWarnVo.setF_sw_id(idsArr[i]);
			sysWarnVo.setF_sw_status("1");
			flag += warnDao.reviewSysWarn(sysWarnVo);
		}
		return flag;
	}
	public int batchUnReviewSysWarn(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			SysWarnVo sysWarnVo = new SysWarnVo();
			sysWarnVo.setF_sw_id(idsArr[i]);
			sysWarnVo.setF_sw_status("2");
			flag += warnDao.reviewSysWarn(sysWarnVo);
		}
		return flag;
	}
	
	public int updateSysWarnStatus(String F_sw_id,String F_sw_status) {
		int flag = 0;
		flag = warnDao.updateSysWarnStatus(F_sw_id,F_sw_status);
		return flag;
	}
}
